//
//  AssortmentViewController.m
//  A_Comic
//
//  Created by sos on 16/1/21.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "AssortmentViewController.h"
#import "ClassflyCollectionCell.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "HomePageModel.h"
#import "MoreViewController.h"
@interface AssortmentViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,retain) UICollectionView *collectionView;
@property (nonatomic,retain) NSMutableArray *dataArr;
@end

@implementation AssortmentViewController
- (void)dealloc
{
    [_collectionView release];
    [_dataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.itemSize = CGSizeMake((self.view.frame.size.width-40)/3, (self.view.frame.size.height-40-64-49)/3);
    
    
    
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerClass:[ClassflyCollectionCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [layout release];
    [self.collectionView release];
    
    
    [self data];
    
}

-(void)data
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:@"http://mobilev3.ac.qq.com/Classify/comicClassifyListV2ForIos/uin/1055701255/local_version/3.6.1/channel/1001/guest_id/FBB11919-34E9-4168-9491-811B4C0BDBD6" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *arr = responseObject[@"data"];
        self.dataArr = [HomePageModel modelHandlerWithArray:arr];
        [self.dataArr removeLastObject];
        [self.collectionView reloadData];
        
        
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
    
}

//协议方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.dataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ClassflyCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
//    cell.backgroundColor = [UIColor blueColor];
    
    HomePageModel *m = self.dataArr[indexPath.item];
    
    [cell.img sd_setImageWithURL:[NSURL URLWithString:m.classify_url]];
    cell.label.text = m.classify_title;
    
    
        return cell;
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    HomePageModel *m = self.dataArr[indexPath.item];
    
    MoreViewController *more = [[MoreViewController alloc]init];
    
    more.str = m.classify_id;
    [self.navigationController pushViewController:more animated:YES];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
